﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;
using System.Drawing;
using IMCOM.Imagiality.ImageProcessing.Canvas2D.AutoContrast;

namespace IMCOM.Imagiality.ImageProcessing.Contrast
{
    public class ContrastMeasurerGUI : Panel, Canvas2DAutoContrastCalculatorObserver
    {
        private Chart chart;
        private Series series;
        private ChartArea chartArea;

        public ContrastMeasurerGUI()
        {
            series = new Series();
            series.ChartType = SeriesChartType.Line;
            series.BorderWidth = 3;
            series.BorderColor = Color.DarkBlue;

            Dock = DockStyle.Fill;

            chart = new Chart();
            chart.Parent = this;
            chart.Dock = DockStyle.Fill;
            chart.AntiAliasing = AntiAliasingStyles.All;
            chart.TextAntiAliasingQuality = TextAntiAliasingQuality.High;

            chartArea = new ChartArea();
            chartArea.AxisX.Title = "Distance in Pixels";
            chartArea.AxisY.Title = "Luminance";
            chartArea.AxisY.Minimum = 0;
            chartArea.AxisY.Maximum = 255;
            chart.ChartAreas.Add(chartArea);

            chart.Series.Add(series);
        }

        private void InitializeComponent()
        {
            this.SuspendLayout();
            this.ResumeLayout(false);
        }

        public void Notify(Canvas2DAutoContrastCalculator calculator)
        {
            readModelValues(calculator);
        }

        private void readModelValues(Canvas2DAutoContrastCalculator calculator)
        {
            double[] xValues = calculator.GetDistanceArray();
            double[] yValues = calculator.GetColorArray();

            if (xValues.Length > 0)
            {

                chartArea.AxisX.Minimum = 0;
                chartArea.AxisX.Maximum = xValues.Max();
                //chartArea.AxisX.Interval = (int)(xValues.Max() / (xValues.Length - 1));
                chartArea.AxisX.Interval = 20;
            }

            series.Points.DataBindXY(xValues, yValues);
        }


    }
}
